%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               replication_approval.m
%
% Author:             Miguel R. Rueda and Sergio Ascencio
%
% Description:        Generates results of Table 12 Appendix model 3 

% Language:           MATLAB R2013b (8.2.0.701) 64 Bit
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear
path='C:/Users/mrueda/Documents/MATLAB/Party_delegates/R&R/replication/';
data=strcat(path,'panel_matlab.xls');

%Importing data
[data2009,labels]=xlsread(data,'2009');
data=data2009;

d_s=dataset({data(:,:) labels{:}});

%Extended
varsx={'l_d_RC_others' 'appr_diff' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'lpop' 'l_turnout' 'local' 'ldist_city' ...
'l_d_RC_others_sq' 'appr_diff_sq' 'lcasillas_sq' 'school_ic_sq' 'l_diff_PRI_PAN_sq' 'lpop_sq' 'l_turnout_sq' 'ldist_city_sq' ...
'l_d_RC_others_appr_diff' 'l_d_RC_others_lcasillas' 'l_d_RC_others_school_ic' 'l_d_RC_others_l_diff_PRI_PAN' 'l_d_RC_others_lpop' 'l_d_RC_others_l_turnout' 'l_d_RC_others_local' 'l_d_RC_others_ldist_city' ...
'lcasillas_appr_diff' 'school_ic_appr_diff' 'l_diff_PRI_PAN_appr_diff' 'lpop_appr_diff' 'l_turnout_appr_diff' 'local_appr_diff' 'ldist_city_appr_diff' ...
'lcasillas_school_ic' 'lcasillas_l_diff_PRI_PAN' 'lcasillas_lpop' 'lcasillas_l_turnout' 'lcasillas_local' 'lcasillas_ldist_city' ...
'school_ic_l_diff_PRI_PAN' 'school_ic_lpop' 'school_ic_l_turnout' 'school_ic_local' 'school_ic_ldist_city' ...
'l_diff_PRI_PAN_lpop' 'l_diff_PRI_PAN_l_turnout' 'l_diff_PRI_PAN_local' 'l_diff_PRI_PAN_ldist_city' ...
'lpop_l_turnout' 'lpop_local' 'lpop_ldist_city' ...
'l_turnout_local' 'l_turnout_ldist_city' ...
'local_ldist_city' 'ldist_pri' 'gov_PRI' 'ldist_pan' 'gov_PAN'};

varsy={'d_RC_PRI_c' 'd_RC_PAN_c'};
varcl={'state_district'};
varcl2={'id_state'};
[Y,X,CLv]=set_design_mat(d_s,varsy,varsx,[varcl varcl2]);


%States where there is enough variation in dep. var to estimate first stage separately for each party
    states_PRI=[9 11 12 14 17 19 20 22 24 26]';
    states_PAN=[5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 26 27 28 30]';

    CLv_PRI=CLv(:,2);
    CLv_PRI(ismember(CLv_PRI(:,1),setdiff(CLv_PRI(:,1),states_PRI)),1)=1;
    CLv_PAN=CLv(:,2);
    CLv_PAN(ismember(CLv_PAN(:,1),setdiff(CLv_PAN(:,1),states_PAN)),1)=1;
%CLv_PRI and CL_vPAN have state codes for those states where the first stage can be estimated (for each party) and a 1 for all others 

%Remember: when runnning main function without state by state fist stage,
%still include state vectors CLv_PAN and CLv_PRI, for each party

%Results
[beta,hessian,phats,LL,auxLL,Llong]=representatives_game(Y,X,0,CLv_PAN,CLv_PRI);

nReps=500;
[betas_monte_extended,se,pvals,~,~]=bootstrap_game(Y,X,beta,nReps,0,CLv_PAN,CLv_PRI,CLv(:,1));
ses=reshape(se,[length(se)/4,4]);
pvals=reshape(pvals,[length(pvals)/4,4]);
labels={'l_d_RC_others' 'appr_diff' 'lcasillas' 'school_ic' 'l_diff_PRI_PAN' 'lpop' 'l_turnout' 'local' 'ldist_city'};
results=table_game(beta,ses,labels)

